var userSM = {
	init: function(){
		var boss = this;
		
		boss.initLayout();
		boss.initEvent();
	},
	initLayout: function(){
		var boss = this;
		
		var day = moment().format("YYYY-MM-DD");
		var startdate = day + " 00:00";
		var enddate = day + " 23:59";
		var data = "startdate=" + startdate + "&enddate=" + enddate + "&";
		$("#UserNowDay").html("日期：" + day);
		$("#user_date_hidden").val(day);
		boss.initUserStatistics(data);
	},
	initEvent: function(){
		var boss = this;
		
		$("#yearUserBtn").bind("click", function(){
			$("#beforeUserBtn").attr("no", 0);
			$("#nextUserBtn").attr("no", 0);
			var year = moment().format("YYYY");
			var day = moment().format("YYYY-MM-DD");
			$("#UserNowDay").html("日期：" + day);
			$("#user_date_hidden").val(year);
			var startdate = year + "-01-01 00:00";
			var enddate = year + "-12-31 23:59";
			var data = "startdate=" + startdate + "&enddate=" + enddate + "&";
			boss.initUserStatistics(data);
		});
		
		$("#monthUserBtn").bind("click", function(){
			$("#beforeUserBtn").attr("no", 0);
			$("#nextUserBtn").attr("no", 0);
			var month = moment().format("YYYY-MM");
			var days = moment().daysInMonth();
			var day = moment().format("YYYY-MM-DD");
			$("#UserNowDay").html("日期：" + day);
			$("#user_date_hidden").val(month);
			var startdate = month + "-01 00:00";
			var enddate = month + "-" + days + " 23:59";
			var data = "startdate=" + startdate + "&enddate=" + enddate + "&";
			boss.initUserStatistics(data);
		});
		
		$("#dayUserBtn").bind("click", function(){
			$("#beforeUserBtn").attr("no", 0);
			$("#nextUserBtn").attr("no", 0);
			var day = moment().format("YYYY-MM-DD");
			$("#UserNowDay").html("日期：" + day);
			$("#user_date_hidden").val(day);
			var startdate = day + " 00:00";
			var enddate = day + " 23:59";
			var data = "startdate=" + startdate + "&enddate=" + enddate + "&";
			boss.initUserStatistics(data);
		});
		
		$("#beforeUserBtn").bind("click", function(){
			var a = parseInt($(this).attr("no"));
			var b = parseInt($("#nextUserBtn").attr("no"));
			var num = a + 1;
			$(this).attr("no", num);
			var day = moment().add("days", (b - num)).format("YYYY-MM-DD");
			var startdate = day + " 00:00";
			var enddate = day + " 23:59";
			var data = "startdate=" + startdate + "&enddate=" + enddate + "&";
			$("#UserNowDay").html("日期：" + day);
			$("#user_date_hidden").val(day);
			boss.initUserStatistics(data);
		});
		
		$("#nextUserBtn").bind("click", function(){
			var a = parseInt($("#beforeUserBtn").attr("no"));
			var b = parseInt($(this).attr("no"));
			var num = b + 1;
			$(this).attr("no", num);
			var day = moment().add("days", (num - a)).format("YYYY-MM-DD");
			var startdate = day + " 00:00";
			var enddate = day + " 23:59";
			var data = "startdate=" + startdate + "&enddate=" + enddate + "&";
			$("#UserNowDay").html("日期：" + day);
			$("#user_date_hidden").val(day);
			boss.initUserStatistics(data);
		});
	},
	initUserStatistics: function(data){
		var boss = this;
		ManagerGrid.grid({
			url: burl + "/Statistics/StatisticsUser.do",
			Data: data,
			PageSize: 30,
			columns: [{
				text: "序号", field: "ROWNUM"
			}, {
				text: "单位", field: "DEPTNAME"
			}, {
				text: "当日APP安装数", field: "NOW_NUM", total: true, defaultValue: 0
			}, {
				text: "总APP安装数", field: "ALL_NUM", total: true, defaultValue: 0
			}],
			OnRender: function(d){
				return d;
			},
			OnComplete: function(){
				
			}
		}, "user_statistics_grid");
	}
};
$(function(){
	userSM.init();
});